package com.wondersgroup.fjzlpt.console.service.impl.ksxx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.quick.framework.util.ExcelOrWordUtils;
import com.wondersgroup.fjzlpt.console.dao.inter.ksxx.KsxxDAO;
import com.wondersgroup.fjzlpt.console.service.inter.ksxx.KsxxService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by lion on 2017/6/2.
 */
@Service
public class KsxxServiceImpl implements KsxxService {

    @Autowired
    private KsxxDAO ksxxDAO;

    @Override
    public JSONArray queryListByPtjgidAndJb(Map map) {
        return ksxxDAO.queryListByPtjgidAndJb(map);
    }

    /**
     * 分页查询机构科室信息
     * @param
     * @return
     */
    public JSONObject queryPageKsxx(Map map){
        return ksxxDAO.queryPageKsxx(map);
    }

    /**
     * 查询二级所有科室信息
     * @param
     * @return
     */
    public JSONArray querySecondJgKsxx(Map map){
        return ksxxDAO.querySecondJgKsxx(map);
    }

    /**
     * 查询机构科室详细信息
     * @param map
     * @return
     */
    public JSONObject queryJgKsxxDetail(Map map){
        return ksxxDAO.queryJgKsxxDetail(map);
    }

    /**
     * 删除机构科室信息
     * @param map
     * @return
     */
    public JSONObject deleteJgKsxx(Map map){
        ResultObject ro = new ResultObject();
        try{
            String PTJGID = (String)map.get("PTJGID");
            String KEYWORD = (String)map.get("KEYWORD");
            ksxxDAO.deleteOneAndTwo(PTJGID, KEYWORD);
        }catch (Exception e){
            e.printStackTrace();
            ro.changeFaultState("删除机构科室失败");
            throw new RuntimeException("删除机构科室失败", e);
        }
        return (JSONObject) JSON.toJSON(ro);
    }

    /**
     * 保存机构科室信息
     * @param map
     * @return
     */
    public JSONObject saveKsxx(Map map){
        ResultObject ro = new ResultObject();
        String method = (String)map.remove("method");
        if("add".equals(method))
        {
            insert(map, ro);
        }
        else
        {
            update(map, ro);
        }
        return (JSONObject) JSON.toJSON(ro);
    }

    private void insert(Map map, ResultObject ro){
        try{
            if(!ksxxDAO.isExistByPrimaryKey(map)){
                String KSDM = (String)map.get("KSDM");
                String SJKSDM = (String)map.get("SJKSDM");
                if(StringUtils.isEmpty(SJKSDM)){
                    SJKSDM = "";
                }
                String KEYWORD = SJKSDM + KSDM;
                map.put("KEYWORD", KEYWORD);
                ksxxDAO.insert(map);
            }else{
                ro.changeFaultState("科室的代码已存在，请重新输入");
            }
        }catch (Exception e){
            e.printStackTrace();
            ro.changeFaultState("新增机构科室失败");
            throw new RuntimeException("新增机构科室失败", e);
        }
    }

    private void update(Map map, ResultObject ro){
        try{
            ksxxDAO.updateForPrimaryKey(map);
        }catch (Exception e){
            e.printStackTrace();
            ro.changeFaultState("修改机构科室失败");
            throw new RuntimeException("修改机构科室失败", e);
        }
    }

    /**
	 * 将机构科室详细信息导出到Excel
	 * @param map
	 * @param request
	 * @param response
	 */
	@Override
	public void downloadToExcel(Map map, HttpServletRequest request, HttpServletResponse response) {
		JSONArray dataArr = ksxxDAO.queryAllForExcel(map);
		JSONObject sftsksObj = JSONObject.parseObject("{'0':'否','1':'是'}");
		for(int i=0;i<dataArr.size();i++){
            JSONObject item = dataArr.getJSONObject(i);
            item.put("YJSFTSKS",sftsksObj.getString(item.getString("YJSFTSKS")));
        }
		//表头
		String titleStr = "{'YLJGMC':'医疗机构名称','YJKSMC':'科室名称','BZKSMC':'标准科室名称','YJSFTSKS':'是否特色科室'," +
                "'EJKSMC':'二级科室名称','EJKSDM':'二级科室代码'}";
		JSONObject title = JSONObject.parseObject(titleStr);
		dataArr.add(0, title);
		
		//排列循序
		List<String> titleField = new ArrayList<>();
		titleField.add("YLJGMC");
		titleField.add("YJKSMC");
		titleField.add("BZKSMC");
		titleField.add("YJSFTSKS");
		titleField.add("EJKSMC");
		titleField.add("EJKSDM");

		ExcelOrWordUtils.downLoadExcelFile(dataArr, titleField, "医疗机构列表", request, response);
		
	}
}
